package com.aischool.service;

import java.util.Date;
import java.util.List;

import com.aischool.utils.StringUtil;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class AiRollCallService {
	
	@Inject
	WeixinUserService weixinUserService;

	public List<Record> getRollCall(int class_id, Date curdate){
		return Db.find("SELECT a.* " + 
				"FROM ai_roll_call a " + 
				"WHERE a.class_id = ? AND a.add_time = ?", class_id, curdate);
	}
	
	public List<Record> getRollCallForStudent(int student_id, String str_yearmonth){
		return Db.find("SELECT a.* " + 
				"FROM ai_roll_call a " + 
				"WHERE a.student_id = ? AND TO_CHAR(a.add_time, 'YYYYMM') = ?", student_id, str_yearmonth);
	}
	
	public int saveRollCall(int add_userid, int class_id, Date cur_date, String uncall_idvs, String change_idvs, String nocall_idvs) {
		int ir = 0;
		if (StrKit.notBlank(uncall_idvs)) {
			// 点名
			String ids1="", ids2="", ids3="";
			String[] array1 = StringUtil.trim(uncall_idvs, ';').split(";");
			for(int i=0; i<array1.length; i++) {
				String[] array2 = array1[i].split(",");
				if (array2[1].equals("1")) { ids1 += array2[0] + ","; }
				else if (array2[1].equals("2")) { ids2 += array2[0] + ","; }
				else if (array2[1].equals("3")) { ids3 += array2[0] + ","; }
			}
			String sql = "INSERT INTO ai_roll_call(class_id, student_id, add_userid, add_time, roll_call_state) " + 
					"SELECT ?, CAST(tt.ids AS INTEGER), ?, ?, ? " + 
					"FROM (SELECT regexp_split_to_table(COALESCE(?,''),E',') AS ids) AS tt";
			
			if (StrKit.notBlank(ids1)) {
				ir += Db.update(sql, class_id, add_userid, cur_date, 1, StringUtil.trim(ids1, ','));
			}
			if (StrKit.notBlank(ids2)) {
				ir += Db.update(sql, class_id, add_userid, cur_date, 2, StringUtil.trim(ids2, ','));
			}
			if (StrKit.notBlank(ids3)) {
				ir += Db.update(sql, class_id, add_userid, cur_date, 3, StringUtil.trim(ids3, ','));
			}
		}
		if (StrKit.notBlank(change_idvs)) {
			// 更改点名

			String[] array1 = StringUtil.trim(change_idvs, ';').split(";");
			Object[][] objH = new Object[array1.length][4];
			for(int i=0; i<array1.length; i++) {
				String[] array2 = array1[i].split(",");
				
				objH[i][0] = Integer.parseInt(array2[1]);
				objH[i][1] = class_id;
				objH[i][2] = Integer.parseInt(array2[0]);
				objH[i][3] = cur_date;
			}
			String sql = "UPDATE ai_roll_call SET roll_call_state = ? "
					+ "WHERE class_id = ? AND student_id = ? AND add_time = ? "
					+ "AND roll_call_state > 0 AND roll_call_state<5";
			int[] irH = Db.batch(sql, objH, 50);
			for(int i=0; i<irH.length; i++) {
				ir += irH[i];
			}
		}
		if (StrKit.notBlank(nocall_idvs)) {
			// 缺勤
			
		}
		return ir;
	}
	
	public int leaveCall(int add_userid, int class_id, Date cur_date, String unleave_idvs) {
		int ir = 0;
		String ids="";
		String[] array1 = StringUtil.trim(unleave_idvs, ';').split(";");
		for(int i=0; i<array1.length; i++) {
			String[] array2 = array1[i].split(",");
			ids += array2[0] + ",";
		}
		String sql = "INSERT INTO ai_roll_call(class_id, student_id, add_userid, add_time, roll_call_state) " + 
				"SELECT ?, CAST(tt.ids AS INTEGER), ?, ?, ? " + 
				"FROM (SELECT regexp_split_to_table(COALESCE(?,''),E',') AS ids) AS tt";
		if (StrKit.notBlank(ids)) {
			ir += Db.update(sql, class_id, add_userid, cur_date, 5, StringUtil.trim(ids, ','));
			
			if (ir>0) {
				// 发送微信通知
				weixinUserService.sendLeaveNotes(class_id, cur_date);
			}
		}
		
		return ir;
	}
}
